package com.mgr;


import com.qy.DaoMgr;
import com.qy.bean.Option;
import com.qy.bean.PayInfo;
import com.qy.util.RechargeUtil;
import com.qy.util.StaticData;
import com.qy.util.TimeUtil;

public class UserPayMgr {
//    //用户订单
//    private static Map<Integer,PayInfo> userPayMap = new ConcurrentHashMap<Integer,PayInfo>(800);
    
    /**
     * 生成一个pay订单
     * @param userId 用户id
     * @param orderType 订单类型
     * @param orderId   绑定的订单号
     * @param orderPrice 订单价格
     * @param voucherId	 优惠券id
     * @param localOrderNum 本地订单号
     * @return PayInfo
     * @throws Exception 
     */
    public static PayInfo createPay(int userId,int orderType,int orderId,int orderPrice,
	    int voucherId) throws Exception{
	PayInfo payInfo = new PayInfo();
	payInfo.setOrderType(orderType);
	payInfo.setOrderId(orderId);
	payInfo.setUserId(userId);
	payInfo.setOrderPrice(orderPrice);
	payInfo.setVoucherId(voucherId);
	payInfo.setPayStatus(StaticData.PayInfoStatus.CREATE.getType());
	payInfo.setCreateDate(TimeUtil.getSysteCurTime());
	payInfo.setLocalOrderNum(getLocalOrderNum(payInfo));
	
	payInfo.setOp(Option.Insert);
	
	DaoMgr.getPayInfoDao().createPayInfo(payInfo);
	
	return payInfo;
    }
    
    public static PayInfo getPay(int id){
	return DaoMgr.getPayInfoDao().getPayInfoById(id);
    }
    
    public static void update(PayInfo payInfo){
	DaoMgr.getPayInfoDao().updatePayInfo(payInfo);
    }
    /**
     * 本地订单号
     * @param payInfo
     * @return
     * @throws Exception
     */
    public static String getLocalOrderNum(PayInfo payInfo) throws Exception{
	return RechargeUtil.encoderByMd5(payInfo.getOrderType()
		+StaticData.first_split+payInfo.getUserId()
		+StaticData.first_split+payInfo.getOrderId()
		+StaticData.first_split+(payInfo.getCreateDate().getTime()/1000));
    }
}
